import os

from pydantic_settings import BaseSettings, SettingsConfigDict

os.environ["MYAPP_app_name"]="app name in os env"


class DatabaseSettings(BaseSettings):
    host: str
    model_config = SettingsConfigDict(
        env_prefix="DB_",  # 指定环境变量前缀
        env_file="./.env",  # 指定配置文件路径
        extra='allow'
    )
class Settings(BaseSettings):
    app_name: str = "MyApp"
    database_url: str
    db: DatabaseSettings = DatabaseSettings()

    model_config = SettingsConfigDict(
        env_prefix="MYAPP_",  # 指定环境变量前缀
        env_file="./.env",  # 指定配置文件路径
        extra='allow'  # 忽略多余配置
    )

# 从环境变量和 .env 文件中解析配置
settings = Settings()
print(settings.app_name)
print(settings.database_url)
print(settings.db.host)